home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / java / cmdLineArgs / example / ParseCmdLine.java < prev   
Encoding:
Java Source  |  1997-07-13  |  2.5 KB  |  69 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. class ParseCmdLine {
  18.     public static void main(String[] args) {
  19.  
  20.         int i = 0, j;
  21.         String arg;
  22.         char flag;
  23.         boolean vflag = false;
  24.         String outputfile = "";
  25.  
  26.         while (i < args.length && args[i].startsWith("-")) {
  27.             arg = args[i++];
  28.  
  29.     // use this type of check for "wordy" arguments
  30.             if (arg.equals("-verbose")) {
  31.                 System.out.println("verbose mode on");
  32.                 vflag = true;
  33.             }
  34.  
  35.     // use this type of check for arguments that require arguments
  36.             else if (arg.equals("-output")) {
  37.                 if (i < args.length)
  38.                     outputfile = args[i++];
  39.                 else
  40.                     System.err.println("-output requires a filename");
  41.                 if (vflag)
  42.                     System.out.println("output file = " + outputfile);
  43.             }
  44.  
  45.     // use this type of check for a series of flag arguments
  46.             else {
  47.                 for (j = 1; j < arg.length(); j++) {
  48.                     flag = arg.charAt(j);
  49.                     switch (flag) {
  50.                     case 'x':
  51.                         if (vflag) System.out.println("Option x");
  52.                         break;
  53.                     case 'n':
  54.                         if (vflag) System.out.println("Option n");
  55.                         break;
  56.                     default:
  57.                         System.err.println("ParseCmdLine: illegal option " + flag);
  58.                         break;
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.         if (i == args.length)
  64.             System.err.println("Usage: ParseCmdLine [-verbose] [-xn] [-output afile] filename");
  65.         else
  66.             System.out.println("Success!");
  67.     }
  68. }
  69.